$(document).ready(function(){
    $.ajax({
        url: '/place/get-points',
        dataType : "json",
        success: function (data, textStatus) {
           // This code allows to calculate distance with no respect to driving mode
            var distance = 0;
            var origin = data.origin;
            var distanations = data.distanations;
            // calculate correct distanation
            $.each(distanations, function(i, val) {
                distance += google.maps.geometry.spherical.computeDistanceBetween(new google.maps.LatLng(origin.lat, origin.lng),
                                                                                  new google.maps.LatLng(50.087692, 14.421150));
                console.log(distance);
            });
        },
//
//==============THIS CODE CALCULATES DISTANCE WITH RESPECT TO TRAVEL MODE
//        success: function (data, textStatus) {
//         // Retrieve the start and end locations and create
//         // a DirectionsRequest using WALKING directions.
//            var origin = data.origin;
//            origin = new google.maps.LatLng(origin.lat, origin.lng);
//            var destinationB = new google.maps.LatLng(50.087692, 14.421150);
//
//            var service = new google.maps.DistanceMatrixService();
//            service.getDistanceMatrix(
//            {
//               origins: [origin],
//               destinations: [destinationB],
//               travelMode: google.maps.TravelMode.DRIVING,
//               avoidHighways: false,
//               avoidTolls: false
//            }, callback);
//
//      function callback(response, status) {
//         console.log(response);
//        if (status != google.maps.DistanceMatrixStatus.OK) {
//          alert('Error was: ' + status);
//        } else {
//          var origins = response.originAddresses;
//          var outputDiv = document.getElementById('outputDiv');
//          outputDiv.innerHTML = '';
//          for (var i = 0; i < origins.length; i++) {
//            var results = response.rows[i].elements;
//            
//            for (var j = 0; j < results.length; j++) {
//              outputDiv.innerHTML += results[j].distance.text + " in "
//                  + results[j].duration.text + "<br />";
//            }
//          }
//        }
//      }
//    },
        error: function(xhr, textStatus, errorThrown) {
        }
    });


});
//function calcRoute() {
//
//  // First, clear out any existing markerArray
//  // from previous calculations.
//  for (i = 0; i < markerArray.length; i++) {
//    markerArray[i].setMap(null);
//  }
//
//  // Retrieve the start and end locations and create
//  // a DirectionsRequest using WALKING directions.
//  var start = document.getElementById("start").value;
//  var end = document.getElementById("end").value;
//  var request = {
//      origin: start,
//      destination: end,
//      travelMode: google.maps.TravelMode.WALKING
//  };

//  directionsService.route(request, function(response, status) {
//    if (status == google.maps.DirectionsStatus.OK) {
//      var warnings = document.getElementById("warnings_panel");
//      warnings.innerHTML = "" + response.routes[0].warnings + "";
//      directionsDisplay.setDirections(response);
//    }
//  });
//}
